<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
/**
 * 过户
 */

namespace AuctionBack\Controller;

use AuctionCore\AuctionOrder\Signature;
use AuctionCore\CarResource\CarCheck;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\AuctionOrder\Transfer;
use AuctionCore\AuctionOrder\Recheck;
use AuctionCore\CarResource\Car;
use AuctionCore\CarResource\CarModel;
use AuctionCore\CarOwner\Owner;
use AuctionCore\Account\Back\Employee;
use AuctionCore\CarResource\SelfReceiveCar;
use AuctionCore\Finance\Payment;
use AuctionCore\Other\Inbox;
use AuctionCore\Common\Util;
use AuctionCore\Other\XimuApi;
use AuctionCore\CarDealer\MyAuctioned;
use Think\Log;
use AuctionCore\ChangeOrder\ChangeOrder;
use AuctionCore\CarResource\BreachCar;
use AuctionCore\PurchaseOrder\PurchaseOrder;
use AuctionCore\Cashier\Payment as CashierPayment;
use AuctionCore\CarResource\CarSource;
use AuctionCore\Dispute\Dispute;
use AuctionCore\CarDealer\Dealer;


class TransferController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->emp_id = $this->user_info['emp_id'];
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
    }

    public function index()
    {
        $this->display();
    }

    /**
     * 列表
     */
    public function getList()
    {
        $page = I("get.p/d", 1);
        $size = 10;
        $status = I('get.status/d', 1);
        $orderType = I('get.order_type', 'p_order');
        $params = array();
        $params['status'] = $status;
        $params['emp_id'] = $this->user_info['emp_id'];
        $params['start_time'] = I('get.start_time', '');
        $params['end_time'] = I('get.end_time', '');
        $params['order_no'] = trim(I("get.order_no", ''));
        $params['contact_person'] = trim(I("get.contact_person", ''));
        $params['contact_mobile'] = trim(I("get.contact_mobile", ''));
        $params['car_plate_number'] = trim(I("get.car_plate_number", ''));
        $params['order_type'] = $orderType;

        $citys = $this->bc_city;
        $city_code = array();
        foreach ($citys as $key => $value) {
            $city_code[] = $key;
        }
        $bc_city = I("get.bc_city");
        if ($bc_city) {
            $params['bc_city'] = $bc_city;
        } else {
            $params['bc_city'] = $city_code;
        }
        //检查权限
        $rules = "GuoHuChaKanSuoYou";
        $e = new Employee($this->user_info['username']);
        $params['auth'] = $e->check_auth($rules);
        if ($orderType == 'd_order') {
            $result = Transfer::getOrderList($page, $size, $params);
        } else {
            $result = Transfer::transfer_list($page, $size, $params);
        }
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign("params", $params);
        $this->display("Transfer/list");
    }

    /**
     * 过户违约
     */
    public function transfer_violate()
    {
        $order_id = I('post.order_id');
        $dealer_id = I('post.dealer_id');
        $remark = trim(I('post.violate_remark'));
        if ($remark == '') {
            $this->helpJsonResult(500, "请填写违约备注信息");
        }
        $o = new AuOrder($order_id);
        $order_info = $o->detail();
        if (empty($order_info)) {
            $this->helpJsonResult(400, "拍单不存在");
        }
        if ($order_info['is_lock'] == 1) {
            $this->helpJsonResult(500, "拍单已锁定");
        }
        //过户中
        $transfer_log = array();
        $transfer_log['order_id'] = $order_id;
        $transfer_log['dealer_id'] = $dealer_id;
        $transfer_log['emp_id'] = $this->user_info['emp_id'];
        $transfer_log['remark'] = $remark;
        $transfer_log['result'] = 3;
        $params = array();
        // 获取表单信息
        $params['order_id'] = $order_id;
        $params['dealer_id'] = $dealer_id;
        if ($order_info['status'] == 9) {
            $params['issue_type'] = 3;
        } else {
            $params['issue_type'] = 5;
        }
        //业务处理
        $where = [
            'order_id' => $order_id,
            'issue_type' => $params['issue_type'],
            'status' => 0
        ];
        $issueInfo = M("dealer_issue")->where($where)->find();
        if (!empty($issueInfo)) {
            $this->helpJsonResult(500, '有尚未处理的争议！');
        }
        $transfer = Transfer::getTransferByOrderId($order_id);
        //开启事物
        M()->startTrans();
        $res = CarCheck::transfer_dealer_issue($params);
        if (!empty($transfer)) {
            $log_result = M("transfer_log")->where(['transfer_id' => $transfer['transfer_id']])->save([
                'emp_id' => $this->user_info['emp_id'],
                'remark' => $remark,
                'result' => 3
            ]);
        } else {
            $log_result = $o->set_trace_log('transfer_log', $transfer_log);
        }
        if (!$res || !$log_result) {
            M()->rollback();
            $msg = '确认失败或已确认,数据未更新。';
            $this->helpJsonResult(500, $msg);
        }
        M()->commit();
        //记录员工操作日志
        $log = array(
            'emp_id' => $this->user_info['emp_id'],
            'order_id' => $order_id,
            'car_id' => $order_info['car_id'],
            'action' => '过户,msg:过户争议'
        );
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name' => "过户争议",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($order_info['car_id'], $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => "过户争议",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);

        $this->helpJsonResult(200, "确认成功");
    }

    /**
     * 过户失败
     **/
    public function failTransfer()
    {
        $order_id = I("get.id/d");
        //获取订单信息
        $o = new AuOrder($order_id);
        $order = $o->detail();
        //车商id
        $dealerInfo = \AuctionCore\CarDealer\Dealer::get_dealer_by_order_id($order_id);
        $dealer_id = !empty($dealerInfo['dealer_id']) ? $dealerInfo['dealer_id'] : 0;
        //获取车信息
        $car = \AuctionCore\CarResource\Car::getDetailWithChannel($order['car_id']);
        //获取车辆补充说明
        $car['other_info'] = M("car_perform")->where('car_id=' . $order['car_id'])->getField("other_info");

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        //获取车型车牌信息
        $cm = new \AuctionCore\CarResource\CarModel();
        $brand = $cm->get_model_by_id($car['brand_id'], 'brand');
        $series = $cm->get_model_by_id($car['series_id'], 'series');
        $model = $cm->get_model_by_id($car['model_id']);
        $car['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $car['plate'] = C("PLATE_PREFIX." . $car['plate_prefix']) . $car['plate_suffix'];
        $car['pointer_mileage'] = formatMileage($car['pointer_mileage']);
        //获取年检有效期
        $procedure = $cm->get_procedure_by_car_id($order['car_id']);
        $city = new \AuctionCore\Other\Address();
        $location_area = $city->get_name_by_code($car['location_area']);
        $car['reg_city'] = $city->get_name_by_code($car['reg_city']);
        $car['verify_expiry'] = $procedure['verify_expiry'];
        $car['com_insure_expiry'] = $procedure['com_insure_expiry'];
        $car['ill_deduc_point'] = $procedure['ill_deduc_point'];
        $car['forfeit'] = $procedure['forfeit'];
        //车商数据
        $dealer = \AuctionCore\CarDealer\Dealer::getDealerById($dealer_id);
        if(!empty($dealer)){
            $d = new Dealer();
            $dealer = $d->get_contact_mobile_byAntNestfind($dealer);
        }
        if ($order['is_other_channel_deal']) { //如果是违约车辆，其他渠道生成的订单，就要从违约车辆表中获取相应的车商联系人信息
            $breachInfo = BreachCar::getInfoByOtherOrderId($order_id);
            $dealerName = $breachInfo['dealer_name']; //车商联系人
            $dealerPhone = $breachInfo['dealer_phone']; //车商电话
            $dealer['contact_person'] = $dealerName;

            $dealer['contact_mobile'] = $dealerPhone;
        }

        //过户失败信息
        $transfer_log = $o->get_transfer_log(3);
        $followLog = $o->get_transfer_follow_log();
        // 获取过户信息
        $o = new AuOrder($order_id);
        $transferInfo = $o->get_transfer_info();
        $this->assign('location_area', $location_area);
        $this->assign('transfer_log', $transfer_log);
        $this->assign('order', $order);
        $this->assign('transfer_info', $transferInfo);
        $this->assign('car', $car);
        $this->assign('followLog', $followLog);
        $this->assign('dealer', $dealer);
        $this->display();
    }


    /**
     * 过户信息
     */
    public function transferInfo()
    {
        $order_id = I("get.id/d");
        //获取订单信息
        $o = new AuOrder($order_id);
        $order = $o->detail();
        //车商id 
        $dealerInfo = \AuctionCore\CarDealer\Dealer::get_dealer_by_order_id($order_id);
        $dealer_id = !empty($dealerInfo['dealer_id']) ? $dealerInfo['dealer_id'] : 0;

        //获取日志记录中提车人信息
        if ($order['status'] > 9) {
            //$transfer_log = $o->get_transfer_log(2);
            if ($order['status'] >= 11) {
                $transfer_log = $o->get_transfer_log(2);
            } else {
                $transfer_log = $o->get_transfer_log(1);
            }
            //print_r($transfer_log);die();
            $man_id = $transfer_log['man_id'];
            if (empty($man_id)) { // //获取车商自过类型的提车人
                $auctionObj = new \AuctionCore\CarDealer\MyAuctioned($dealer_id);
                $auctionInfo = $auctionObj->get_by_order_id($order_id);
                $man_id = !empty($auctionInfo['man_id']) ? $auctionInfo['man_id'] : 0;
            }
            if ($man_id) {
                //获取对应提车人信息
                $TakeMan = new \AuctionCore\CarDealer\TakeMan($dealer_id);
                $succ_takeman = $TakeMan->detail($man_id);
            }
        } else {
            //过户成功
            $transfer_log = $o->get_transfer_log(1);
        }
        //获取车信息
        $car = \AuctionCore\CarResource\Car::getDetailWithChannel($order['car_id']);
        //获取车辆补充说明
        $car['other_info'] = M("car_perform")->where('car_id=' . $order['car_id'])->getField("other_info");

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

//        //找到4s店联系人联系电话
//        if ($car['car_source'] == 1) {
//            $map = array();
//            $map['channel_id'] = array('eq',$car['comefrom']);
//            $fours_info = \AuctionCore\Fours\Fours::get_fours_info($map);
//            $this->assign('fours_info', $fours_info);
//        }

        //获取车型车牌信息
        $cm = new \AuctionCore\CarResource\CarModel();
        $brand = $cm->get_model_by_id($car['brand_id'], 'brand');
        $series = $cm->get_model_by_id($car['series_id'], 'series');
        $model = $cm->get_model_by_id($car['model_id']);
        $car['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $car['plate'] = C("PLATE_PREFIX." . $car['plate_prefix']) . $car['plate_suffix'];
        $car['pointer_mileage'] = formatMileage($car['pointer_mileage']);
        //获取年检有效期
        $procedure = $cm->get_procedure_by_car_id($order['car_id']);
        $city = new \AuctionCore\Other\Address();
        $location_area = $city->get_name_by_code($car['location_area']);
        $car['reg_city'] = $city->get_name_by_code($car['reg_city']);
        $car['verify_expiry'] = $procedure['verify_expiry'];
        $car['com_insure_expiry'] = $procedure['com_insure_expiry'];
        $car['ill_deduc_point'] = $procedure['ill_deduc_point'];
        $car['forfeit'] = $procedure['forfeit'];
        //获取车主信息
        $co = new Owner($car['owner_id']);
        $owner = $co->detail();
        $owner['owner_type_name'] = C('OWNER_TYPE_NAME')[$owner['owner_type']];
        $dealer = \AuctionCore\CarDealer\Dealer::getDealerById($dealer_id);
        if(!empty($dealer)){
            $d = new Dealer();
            $dealer = $d->get_contact_mobile_byAntNestfind($dealer);
        }
        if ($order['is_other_channel_deal']) { //如果其他渠道生成的订单就要判断是违约车辆的还是自收车辆的
            if ($order['refer'] == 1) { //违约车辆
                $breachInfo = BreachCar::getInfoByOtherOrderId($order_id);
                $dealerName = $breachInfo['dealer_name']; //车商联系人
                $dealerPhone = $breachInfo['dealer_phone']; //车商电话
                $takeCarMan = $breachInfo['take_car_man']; //提车人
            } elseif ($order['refer'] == 2) { //自收车辆
                $selfCarInfo = SelfReceiveCar::getInfoByOtherOrderId($order_id);
                $dealerName = $selfCarInfo['dealer_name']; //车商联系人
                $dealerPhone = $selfCarInfo['dealer_phone']; //车商电话
                $takeCarMan = $selfCarInfo['take_car_man']; //提车人
            }

            $dealer['contact_person'] = $dealerName;
            $dealer['contact_mobile'] = $dealerPhone;
            $dealer['take_car_man'] = $takeCarMan;
        }

        //获取提车人信息
        $take_car_men = \AuctionCore\CarDealer\Dealer::getTakeCarMan($dealer_id);
        //获取订单对应过户图片
        $transfer_img = \AuctionCore\AuctionOrder\AuOrder::getTransferImg($order_id);
        // 获取过户信息
        $o = new AuOrder($order_id);
        $transferInfo = $o->get_transfer_info();
        $transferInfo['transfer_to'] = C("TRANSFER_TO." . $transfer_log['transfer_to']);

        // 获取评估师信息
        $re = new Recheck($order_id);
        $checkerInfo = $re->get_checker_info($owner['checker_id']);
        $host = get_content_cdnurl();
        //图片的url
        $img_url = array();
        foreach ($transfer_img as $key => $value) {
            $img_url[$value['type']] = $host . $value['url'];
        }
        //图片的类型
        $img_type = array();
        foreach ($transfer_img as $key => $value) {
            $img_type[] = $value['type'];
        }
        $alltype = C('TRANSFER_PROCEDURE_TYPE');

        //获取下一步提车人 步骤
        $redis = new \AuctionCore\Common\PhpRedis(0);
        $key = "transfer_" . $order_id;
        $step = $redis->get($key);

        $followLog = $o->get_transfer_follow_log();
        $lastTransPrice = \AuctionCore\Other\Price::getLastTransactionPrice($order_id);
        //付首款备注 pay_log
        $pay_log = $o->get_pay_trace_log($order['car_id']);
        $this->assign('pay_log', $pay_log);

        $this->assign('last_trans_price', $lastTransPrice);
        $this->assign('step', $step);
        $this->assign('follow_log', $followLog);
        $this->assign('transfer_info', $transferInfo);
        $this->assign('checker_info', $checkerInfo);
        $this->assign('location_area', $location_area);
        $this->assign('alltype', $alltype);
        $this->assign('host', $host);
        $this->assign('transfer_img', $transfer_img);
        $this->assign('img_type', $img_type);
        $this->assign('order', $order);
        $this->assign('take_men', $take_car_men);
        $this->assign('succ_takeman', $succ_takeman);
        $this->assign('car', $car);
        $this->assign('owner', $owner);
        $this->assign('dealer', $dealer);
        $this->assign('transfer_log', $transfer_log);
        $this->assign('img_url', $img_url);
        $this->display();
    }

    /**
     * 过户日志信息
     */
    public function transferLog()
    {
        $order_id = I("get.id/d");
        //获取订单信息
        $o = new AuOrder($order_id);
        $order = $o->detail();
        //获取日志记录中提车人信息
        if ($order['status'] >= 8) {
            //过户成功
            $transfer_log = $o->get_transfer_log(1);
            //过户成功-提成人
            $succ_transfer_log = $o->get_transfer_log(2);
            // $man_id = $succ_transfer_log['man_id'];
            // if ($man_id) {
            //     //获取对应提成人信息
            //     $TakeMan = new \AuctionCore\CarDealer\TakeMan($order['success_dealer_id']);
            //     $succ_takeman = $TakeMan->detail($man_id);
            // }
        }
        //获取车信息
        $car = \AuctionCore\CarResource\Car::getDetailWithChannel($order['car_id']);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        //找到4s店联系人联系电话
        if ($car['car_source'] == 1) {
            $map = array();
            $map['channel_id'] = array('eq', $car['comefrom']);
            $fours_info = \AuctionCore\Fours\Fours::get_fours_info($map);
            $this->assign('fours_info', $fours_info);
        }

        //获取车型车牌信息
        $cm = new \AuctionCore\CarResource\CarModel();
        $brand = $cm->get_model_by_id($car['brand_id'], 'brand');
        $series = $cm->get_model_by_id($car['series_id'], 'series');
        $model = $cm->get_model_by_id($car['model_id']);
        $car['model'] = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $car['plate'] = C("PLATE_PREFIX." . $car['plate_prefix']) . $car['plate_suffix'];
        $car['pointer_mileage'] = formatMileage($car['pointer_mileage']);
        //获取年检有效期
        $procedure = $cm->get_procedure_by_car_id($order['car_id']);
        $city = new \AuctionCore\Other\Address();
        $location_area = $city->get_name_by_code($car['location_area']);
        $car['reg_city'] = $city->get_name_by_code($car['reg_city']);
        $car['verify_expiry'] = $procedure['verify_expiry'];
        $car['com_insure_expiry'] = $procedure['com_insure_expiry'];
        $car['ill_deduc_point'] = $procedure['ill_deduc_point'];
        $car['forfeit'] = $procedure['forfeit'];
        //获取车辆补充说明
        $car['other_info'] = M("car_perform")->where('car_id=' . $order['car_id'])->getField("other_info");

        //获取车主信息
        $co = new Owner($car['owner_id']);
        $owner = $co->detail();
        $owner['owner_type_name'] = C('OWNER_TYPE_NAME')[$owner['owner_type']];
        //车商id 
        $dealerInfo = \AuctionCore\CarDealer\Dealer::get_dealer_by_order_id($order_id);

        $dealer_id = !empty($dealerInfo['dealer_id']) ? $dealerInfo['dealer_id'] : 0;

        $dealer = \AuctionCore\CarDealer\Dealer::getDealerById($dealer_id);
        if(!empty($dealer)){
            $d = new Dealer();
            $dealer = $d->get_contact_mobile_byAntNestfind($dealer);
        }
        if ($order['is_other_channel_deal']) { //如果其他渠道生成的订单就要判断是违约车辆的还是自收车辆的
            if ($order['refer'] == 1) { //违约车辆
                $breachInfo = BreachCar::getInfoByOtherOrderId($order_id);
                $dealerName = $breachInfo['dealer_name']; //车商联系人
                $dealerPhone = $breachInfo['dealer_phone']; //车商电话
                $takeCarMan = $breachInfo['take_car_man']; //提车人
            } elseif ($order['refer'] == 2) { //自收车辆
                $selfCarInfo = SelfReceiveCar::getInfoByOtherOrderId($order_id);
                $dealerName = $selfCarInfo['dealer_name']; //车商联系人
                $dealerPhone = $selfCarInfo['dealer_phone']; //车商电话
                $takeCarMan = $selfCarInfo['take_car_man']; //提车人
            }

            $dealer['contact_person'] = $dealerName;
            $dealer['contact_mobile'] = $dealerPhone;
            $dealer['take_car_man'] = $takeCarMan;
        }

        //获取提车人信息
        $take_car_men = \AuctionCore\CarDealer\Dealer::getTakeCarMan($dealer_id);
        //获取订单对应过户图片
        $transfer_img = \AuctionCore\AuctionOrder\AuOrder::getTransferImg($order_id);
        // 获取过户信息
        $o = new AuOrder($order_id);
        $transferInfo = $o->get_transfer_info();
        $transferInfo['transfer_to'] = C("TRANSFER_TO." . $transfer_log['transfer_to']);
        // 获取评估师信息
        $o = new Recheck($order_id);
        $checkerInfo = $o->get_checker_info($owner['checker_id']);

        //图片的类型
        $img_type = array();
        foreach ($transfer_img as $key => $value) {
            $img_type[] = $value['type'];
        }
        $alltype = C('TRANSFER_PROCEDURE_TYPE');
        $host = get_content_cdnurl();
        //获取下一步提车人 步骤
        $redis = new \AuctionCore\Common\PhpRedis(0);
        $key = "transfer_" . $order_id;
        $step = $redis->get($key);

        $o = new AuOrder($order_id);
        $followLog = $o->get_transfer_follow_log();
        $lastTransPrice = \AuctionCore\Other\Price::getLastTransactionPrice($order_id);
        //付收款备注 pay_log
        $pay_log = $o->get_pay_trace_log($order['car_id']);
        $this->assign('pay_log', $pay_log);
        $this->assign('last_trans_price', $lastTransPrice);
        $this->assign('step', $step);
        $this->assign('follow_log', $followLog);
        $this->assign('transfer_info', $transferInfo);
        $this->assign('checker_info', $checkerInfo);
        $this->assign('location_area', $location_area);
        $this->assign('alltype', $alltype);
        $this->assign('host', $host);
        $this->assign('transfer_img', $transfer_img);
        $this->assign('img_type', $img_type);
        $this->assign('order', $order);
        $this->assign('take_men', $take_car_men);
        $this->assign('car', $car);
        $this->assign('owner', $owner);
        $this->assign('dealer', $dealer);
        $this->assign('transfer_log', $transfer_log);
        $this->display();
    }

    /**
     * 过户继续跟踪
     */
    public function confirmTransferFollow()
    {
        $order_id = I('post.order_id/d');
        if ($order_id < 1) {
            $this->helpJsonResult(410, "参数错误，拍单ID不能为空");
        }
        $o = new AuOrder($order_id);
        $order = $o->detail();
        if ($order['is_lock'] == 1) {
            $this->helpJsonResult(500, "拍单已锁定");
        }
        $car_id = I("post.car_id/d");
        if ($car_id < 1) {
            $this->helpJsonResult(408, "汽车ID错误");
        }
        $owner_id = I("post.owner_id/d");
        if ($owner_id < 1) {
            $this->helpJsonResult(408, "车主ID错误");
        }
        $transfer_limit = I('post.transfer_limit');
        if ($transfer_limit == 3) {
            $this->helpJsonResult(415, '请选择过户限制');
        }

        $userGroupInfo = Employee::get_emp_group($this->user_info['emp_id']);

        $remark = I('post.remark');
        $transfer_log = array();
        $transfer_log['order_id'] = $order_id;
        $transfer_log['emp_id'] = $this->user_info['emp_id'];
        $transfer_log['remark'] = $remark;
        // $transfer_log['emp_group'] = '';
        $transfer_log['createtime'] = date("Y-m-d H:i:s", time());

        M()->startTrans();
        /**
         * 记录拍品跟踪日志
         */

        \AuctionCore\AuctionOrder\AuOrder::save_order_trace_follow_log($order_id, $transfer_log);
        //记录员工操作日志
        $log = array(
            'emp_id' => $this->user_info['emp_id'],
            'order_id' => $order_id,
            'car_id' => $car_id,
            'owner_id' => $owner_id,
            'action' => '过户继续跟踪,msg:继续跟踪备注'
        );
        \AuctionCore\System\SystemLog::add($log);
        M()->commit();

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name' => "过户跟踪",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => "过户跟踪",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);


        $this->helpJsonResult(200, "确认成功");
    }


    /**
     * 下一步(提车人)
     **/
    public function next_transfer()
    {
        $order_id = I('post.order_id');
        try {
            $redis = new \AuctionCore\Common\PhpRedis(0);
            $key = "transfer_" . $order_id;
            $redis->set($key, 1, 86400);
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => ''));
            exit;

        } catch (AuctionException $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }

    }

    //修改身份证
    public function eidt_cn()
    {
        $owner_id = I('post.owner_id');
        $certificate_number = I('post.certificate_number');
        if (!preg_match('/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/', $certificate_number)) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 401, 'msg' => '身份证格式错误'));
            exit;
        }
        try {
            M('car_owner')->where(array('owner_id' => $owner_id))->save(array('certificate_number' => $certificate_number));
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => ''));
            exit;
        } catch (AuctionException $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }

    /**
     *转户图片上传
     **/
    public function add_transfer_img()
    {
        $order = I('post.order_id');
        $type = I('post.type');
        $pic = I('data.pic', '', '', $_FILES);
        // var_dump($pic);
        // exit;
        $size = $pic['size'];
        $imagetype = $pic['type'];
        $exts = array('image/jpg', 'image/gif', 'image/png', 'image/jpeg');
        $maxSize = 5120000;
        //尾缀验证图片类型
        $ext = get_extension($pic['name']);
        $exts = array('jpg', 'png', 'jpeg');
        if ($size > $maxSize) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 500, 'msg' => '图片不能超过5M'));
            exit;
        }
        if (!in_array($ext, $exts)) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 501, 'msg' => '上传图片格式不正确'));
            exit;
        }
        if (!empty($pic)) {
            $flag = true;
            if ($pic['error'] != 0) {
                $flag = false;
                $rep['msg'] = '照片上传失败';
                \Think\Log::record($rep['msg'] . ',错误码：' . $pic['error'] . ',time:' . date('Y-m-d H:i:s'));
            }
            //图片上传
            if ($flag) {
                $object = 'upload/' . create_uuid() . '.' . get_extension($pic['name']);
                $tmp_path = '/tmp/' . create_uuid() . '.' . get_extension($pic['name']);;
                // $pic_path = $pic['tmp_name'];
                //压缩图片
                if ($ext == 'jpg') {
                    $Source = imagecreatefromjpeg($pic['tmp_name']);
                    $image = Imagejpeg($Source, $tmp_path, 30);
                }
                if ($ext == 'jpeg') {
                    $Source = Imagecreatefromjpeg($pic['tmp_name']);
                    $image = Imagejpeg($Source, $tmp_path, 30);
                }
                if ($ext == 'png') {
                    $Source = Imagecreatefrompng($pic['tmp_name']);
                    $image = imagepng($Source, $tmp_path);
                }
                ImageDestroy($Source);
                try {
                    $aliObject = \AuctionCore\Common\Image::upload_file_by_file(OSS_BUCKET, $object, $tmp_path);
                    if ($aliObject) {
                        $data['url'] = $aliObject;
                        $data['order_id'] = $order;
                        $data['type'] = $type;
                        $data['createtime'] = date('Y-m-d H:i:s');
                        $transfer_img = M("transfer_img");
                        $img_id = $transfer_img->data($data)->add();
                    }
                    $host = get_content_cdnurl();
                    $url = $host . $aliObject;
                    $arr = array(
                        'pic' => $url,
                        'img_id' => $img_id
                    );
                    //删除压缩后的临时文件
                    @unlink($tmp_path);
                    echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => $arr));
                    exit;
                } catch (AuctionException $e) {
                    echo Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
                    exit;
                }
            }
        }
    }

    /**
     *转户图片删除
     **/
    public function del_transfer_img()
    {
        try {
            $id = I('post.img_id');
            if ($id >= 1) {
                // $host = get_content_cdnurl();
                // $url = M('transfer_img')->where("id=".$id)->find();
                // @unlink($host.$url['url']);
                $del = M('transfer_img')->where("id=" . $id)->delete();
            } else {
                throw new AuctionException('操作非法！');
            }
            echo Util::auto_jsonp_or_json($_REQUEST, array('code' => 200, 'msg' => '', 'data' => ''));
            exit;
        } catch (AuctionException $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, array('msg' => $e->getMessage(), 'code' => $e->getCode()));
            exit;
        }
    }


    /**
     * 确认开始过户
     */
    public function confirmTransfer()
    {
        $order_id = I('post.order_id/d');
        if ($order_id < 1) {
            $this->helpJsonResult(400, "参数错误，拍单ID不能为空");
        }
        $o = new AuOrder($order_id);
        $order = $o->detail();
        if ($order['is_lock'] == 1) {
            $this->helpJsonResult(500, "拍单已锁定");
        }
        $car_id = I("post.car_id/d");
        if ($car_id < 1) {
            $this->helpJsonResult(400, "汽车ID错误");
        }
        $owner_id = I("post.owner_id/d");
        if ($owner_id < 1) {
            $this->helpJsonResult(400, "车主ID错误");
        }
        $transfer_limit = I('post.transfer_limit/d', 0);
        if ($transfer_limit == 0 || $transfer_limit == 3) {
            $this->helpJsonResult(400, '请选择车辆落户地');
        }
        $remark = I('post.reauction_remark');

        $transfer_log = array();
        $transfer_log['order_id'] = $order_id;
        $transfer_log['emp_id'] = $this->user_info['emp_id'];
        $transfer_log['remark'] = $remark;
        $transfer_log['transfer_to'] = $transfer_limit;
        $transfer_log['result'] = 1;
        $transfer_log['last_update'] = date('Y-m-d H:i:s', time());
        $transfer = Transfer::getTransferByOrderId($order_id);
        //开启事务
        M()->startTrans();
        $o = new AuOrder($order_id);
        $result = $o->update(array('status' => 10));
        if (!empty($transfer)) {
            unset($transfer_log['order_id']);
            $log_result = M("transfer_log")
                ->where(['transfer_id' => $transfer['transfer_id']])
                ->save($transfer_log);
        } else {
            $log_result = $o->set_trace_log('transfer_log', $transfer_log);
        }
        if (!$result || !$log_result) {
            M()->rollback();
            $msg = '确认失败或已确认,数据未更新。';
            $this->helpJsonResult(500, $msg);
        }
        $car = new Car($car_id);
        $carInfo = $car->detail();
        $CarSource = new CarSource($carInfo['sid']);
        $csData = [];
        $csData['status'] = 402;
        $res_cs = $CarSource->update($csData);

        M()->commit();
        //记录员工操作日志
        $log = array(
            'emp_id' => $this->user_info['emp_id'],
            'order_id' => $order_id,
            'car_id' => $car_id,
            'owner_id' => $owner_id,
            'action' => '过户,msg:确认开始过户'
        );
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name' => "开始过户",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => "开始过户",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);

        $this->helpJsonResult(200, "确认成功");
    }

    /**
     * 完成过户
     */
    public function finishTransfer()
    {
        $order_id = I('post.order_id/d');
        //$order_id = 797;
        if ($order_id < 1) {
            $this->helpJsonResult(410, "参数错误，拍单ID不能为空");
        }
        $car_id = I("post.car_id/d");
        if ($car_id < 1) {
            $this->helpJsonResult(409, "汽车ID错误");
        }
        $owner_id = I("post.owner_id/d");
        if ($owner_id < 1) {
            $this->helpJsonResult(408, "车主ID错误");
        }
        $man_id = 0;
        $o = new AuOrder($order_id);
        $order = $o->detail();
        if ($order['is_lock'] == 1) {
            $this->helpJsonResult(500, "拍单已锁定");
        }
        $transferInfo = $o->get_transfer_info();

        $is_other_channel_deal = $transferInfo['is_other_channel_deal'];
        if (!$is_other_channel_deal) { //如果不是其他渠道提交，就要判断是否有提车人
            $man_id = I("post.man_id");
            if (empty($man_id)) {
                $this->helpJsonResult(404, "提车人不能为空");
            }
        }

        $dealerInfo = \AuctionCore\CarDealer\Dealer::get_dealer_by_order_id($order_id);
        $dealer_id = !empty($dealerInfo['dealer_id']) ? $dealerInfo['dealer_id'] : 0;

        $auctionObj = new \AuctionCore\CarDealer\MyAuctioned($dealer_id);
        $auctionObj->up_by_order_id($order_id, array('man_id' => $man_id));

        $remark = I("post.remark");
        $now = date("Y-m-d H:i:s");
        $car = new Car($car_id);
        $car_detail = $car->detail();
        //开启事务
        M()->startTrans();
        $data = array();
        if ($is_other_channel_deal) { //如果是其他渠道成交，就表示是订单。则过户完成，就表示拍单完成
            $data['status'] = 13;
        } else {
            $data['status'] = 11;
        }

        //4s店来的车或已付过尾款的个人来源，跳过付尾款流程
        if ($car_detail['car_source'] == '1') {
            $data['status'] = 13;
            $data['trade_time'] = $now;
            $car_data = array();
            $car_data['car_status'] = 9;
            $car_res = $car->updateCar($car_data);
            if (!$car_res) {
                M()->rollback();
                $this->helpJsonResult(500, "确认失败或数据已更新");
            }

            //采购单完成
            $resPurchaseOrder = PurchaseOrder::finishBySid($car_detail['sid']);
            if (!$resPurchaseOrder) {
                Log::record("过户完成:采购单完成失败，sid {$car_detail['sid']}", "ERR", true);
            }
        } else { //个人流程
            //创建付款单
            $this->createPaymentOrder($car_detail, $order_id);
            $res = $car->updateCar(['pay_status' => 3]); //更改车源状态 待付尾款
            if (!$res) {
                M()->rollback();
                $this->helpJsonResult(500, "更新车辆付款状态失败");
            }
        }


        //如果自收车辆则到自收车辆交易成功列表中
        if ($car_detail['is_self_receive'] == 1) {
            $self_receive_car = new \AuctionCore\CarResource\SelfReceiveCar();
            $self_receive_car_ret = $self_receive_car->add_self_receive_car(
                [
                    'car_id' => $order['car_id'],
                    'order_id' => $order_id,
                    'trade_price' => $order['success_price'],
                    'status' => 3,
                    'deal_time' => $now,
                ]
            );
            if (!$self_receive_car_ret) {
                M()->rollback();
                $this->helpJsonResult(500, "添加自收车辆失败");
            }
        }
        $result = $o->update($data);

        $trans_log = Transfer::getTransferByOrderId($order_id);
        $log_data = array(
            'emp_id' => $this->user_info['emp_id'],
            'company' => $trans_log['company'],
            'phone' => $trans_log['phone'],
            'contact' => $trans_log['contact'],
            'idcard' => $trans_log['idcard'],
            'man_id' => $man_id,
            'remark' => $remark,
            'result' => 2,
        );
        if (!empty($trans_log)) {
            $log_result = M("transfer_log")->where(['transfer_id' => $trans_log['transfer_id']])->save($log_data);
        } else {
            $log_result = $o->set_trace_log('transfer_log', $log_data);
        }
        //徙木贷款取消
//        $loan_log = $o->get_loan_log();
//        if ($loan_log) {
//            $product_id = C('PROD_CAR_OLD_CLCW.0');//产品ID
//            $should_pay = $loan_log['loan_money'] + $loan_log['first_pay_money'];
//            $loan_res = $this->get_transaction_limit($order_id, $should_pay);
//            $data1 = array();
//            $order_info = $o->detail();
//            $data1['xm_list_code'] = $loan_log['xm_list_code'];
//            $data1['first_pay_capi'] = (string)($loan_log['first_pay_money'] * 100);
//            $data1['product_id'] = $product_id;
//            $lowest_ratio = 1 - $loan_res['data']['bal_rate'];
//            $data1['lowest_ratio'] = sprintf("%.1f", $lowest_ratio);
//            $data1['org_book_no'] = $order_info['order_no'];
//            $data1['receive_date'] = date('Y-m-d H:i:s');
//            // var_dump($data1);
//            // die();
//            $ximu = new XimuApi();
//            $loan_firstpay = $ximu->_get('ORDER', 'confirmfirstpay', $data1);
//            if ($loan_firstpay['code'] != 200) {
//                M()->rollback();
//                Log::record("过户完成,首付款确认失败。 order_id: {$order_id}", "ERR", true);
//                $this->helpJsonResult(500, '确认失败或已确认,数据未更新。');
//            }
//            $data2 = array(
//                'loan_status' => '2',
//                'firstpaytime' => date('Y-m-d h:i:s')
//            );
//            $loan_status = $o->update_loan_log($data2);
//            if (!$loan_status) {
//                M()->rollback();
//                Log::record("过户完成,更新贷款记录失败。 order_id: {$order_id}", "ERR", true);
//                $this->helpJsonResult(500, '确认失败或已确认,数据未更新。');
//            }
//        }
        //找到对应未作废的变更单
        $changeInfo = ChangeOrder::getChangeInfoBySid($car_detail['sid']);
        $res_change = true;
        if ($changeInfo) {
            //如果变更信息存在，将变更单对应付款状态由-1改成0
            foreach ($changeInfo as $key => $value) {
                $res_change = CashierPayment::changeStatusByMid($value['mid']);
            }
        }
        //更新车源为完成状态
        $CarSource = new CarSource($car_detail['sid']);
        $csData = [];
        $csData['status'] = 500;
        $CarSource->update($csData);

        if (!$result || !$log_result || !$res_change) {
            M()->rollback();
            $this->helpJsonResult(500, '确认失败或已确认,数据未更新。');
        }
        M()->commit();
        //解冻保证金
        \AuctionCore\CarDealer\AssetCenter::free_all($order_id);
        //提车人数量增加
        if ($man_id > 0) {
            \AuctionCore\CarDealer\TakeMan::updateTakeNum($man_id);
        }
        //发放抽奖卡卷
        $Raffle = new \AuctionCore\Other\Raffle();
        $Raffle->order_binding_card($order_id, 2);
        //删除redis中kv
        $redis = new \AuctionCore\Common\PhpRedis(0);
        $key = "transfer_" . $order_id;
        $redis->del($key);

        //发送消息
        $uid = $o->get_uid_by_order();
        $car_name = $car->get_car_name();
        $content = "【 " . $car_name . " 】拍单编号：" . $order['order_no'] . "，过户成功。敬请到店收车和取回过户资料。";
        Inbox::sendMessage($uid, '交易成功', $content);

        //记录员工操作日志
        $log = array(
            'emp_id' => $this->user_info['emp_id'],
            'order_id' => $order_id,
            'car_id' => $car_id,
            'owner_id' => $owner_id,
            'action' => '过户,msg:确认开始过户'
        );
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name' => "过户完成",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => "过户完成",
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);

        $this->helpJsonResult(200, "确认成功");
    }


    /**
     * 创建付款单
     *
     * @param array $car_detail 车源信息
     * @param int $order_id 拍单ID
     */
    private function createPaymentOrder($car_detail, $order_id)
    {
        $sid = $car_detail['sid'];
        $purchaseOrder = new PurchaseOrder();
        $purchaseOrderData = $purchaseOrder->viewBySid($sid);

        $purchaseOrderId = $purchaseOrderData['id'];
        $totalAmount = $purchaseOrderData['amount']; //收款总金额
        $paymentData = [];
        $signInfo = Signature::getInfoByOrderId($purchaseOrderData['order_id']);
        $paymentData['bank_name'] = $signInfo['deposit_bank']; //收款银行
        $paymentData['card_no'] = $signInfo['payee_account']; // 收款账号
        $paymentData['account_name'] = $signInfo['payee_name']; //收款人姓名
        $paymentData['pay_type'] = 3; // '采购应付（尾）',

        if ($car_detail['three_in_one'] == 1) {//如果三证合一 收50%尾款
            $paymentData['amount'] = $totalAmount / 2; //个人车源 50%首款
        } elseif ($car_detail['three_in_one'] == 2) {
            $paymentData['amount'] = $totalAmount; //个人车源 收100% 全款
        }

        $res6 = $this->generatorPayment($paymentData);//添加付款单
        if (!$res6) {
            M()->rollback();
            $this->helpJsonResult(500, "添加付款单信息失败");
        }
        $res7 = $this->addPaymentRelation($purchaseOrderId, $res6, $paymentData['pay_type']);
        if (!$res7) {
            M()->rollback();
            $this->helpJsonResult(500, "添加付款单采购单关系失败");
        }
    }

    /**
     * 添加付款单
     * @param array $paymentData
     * @return mixed
     */
    private function generatorPayment($paymentData = array())
    {
        $paymentData['pay_way'] = 2;// 转账
        $paymentData['create_time'] = date('Y-m-d H:i:s');//创建时间
        $result = CashierPayment::add($paymentData);
        return $result;
    }

    /**
     * 添加采购单付款关系数据
     *
     * @param int $purchaseOrderId 采购单编号
     * @param int $paymentId 付款单编号
     * @param int $paymentType 付款类型
     * @return mixed
     */
    public function addPaymentRelation($purchaseOrderId, $paymentId, $paymentType)
    {
        $purchaseOrder = new PurchaseOrder();
        return $purchaseOrder->addPaymentAndPurchaseOrderRelation($purchaseOrderId, $paymentId, $paymentType);
    }

    /**
     * 3.5. 交易额度查询接口 公用
     *
     * @author luhao 16-08-30
     */
    public function get_transaction_limit($order_id, $should_pay)
    {
        $o = new AuOrder($order_id);
        $order_info = $o->detail();
        $d = new MyAuctioned($order_info['success_dealer_id']);
        $auctioned = $d->get_by_order_id($order_id);
        $car = new Car($order_info['car_id']);
        $car_info = $car->detail();
        $cm = new CarModel();
        //查询可贷款金额
        $map = array();
        $map['product_id'] = C('PROD_CAR_OLD_CLCW.0');
        $map['out_user_code'] = $order_info['success_dealer_id'];
        $map['car_deal_price'] = (string)($should_pay * 100);
        $map['car_id'] = $car_info['car_no'];
        //车辆信息
        $map['car_id'] = $car_info['car_no'];//车辆编号
        $brand_name = $cm->get_model_by_id($car_info['brand_id'], 'brand');
        $series_name = $cm->get_model_by_id($car_info['series_id'], 'series');
        $model_name = $cm->get_model_by_id($car_info['model_id'], 'model');
        //车辆手续信息
        $car_procedure = $cm->get_procedure_by_car_id($order_info['car_id']);

        $map['car_mark_sign'] = $brand_name['name'] ? $brand_name['name'] : '无';//品牌
        $map['car_serial'] = $series_name['name'] ? $series_name['name'] : '无';//车系
        $map['car_model'] = $model_name['name'] ? $model_name['name'] : '无';//车款
        $map['car_out_year'] = $car_info['ex_factory_date']; //出厂年月
        $map['car_no'] = C('PLATE_PREFIX')[$car_info['plate_prefix']] . $car_info['plate_suffix']; //车牌号
        $map['car_chejia'] = $car_info['vin'];//车架
        $map['car_engi_no'] = $car_info['engine_no'];//发动机号
        $map['car_run_mile'] = $car_info['pointer_mileage'];//里程
        $map['car_no_date'] = $car_info['first_reg_date'] . " 00:00:00";//上牌日期
        $map['ns_date_to'] = $car_procedure['verify_expiry'] . " 00:00:00";//年审检验到期日
        $map['car_end_insurance'] = $car_procedure['com_insure_expiry'] . " 00:00:00";//交强险截止日期
        $map['scorerating'] = $car_info['composite_state'] . $car_info['accident_level'];//评分等级
        $map['car_ep'] = C('EMISSION')[$car_info['emission']]; //环保标准
        $map['car_useage'] = C('USE_PROPERTIES')[$car_info['use_properties']];//用途
        $map['car_pailiang'] = $car_info['car_cc'];//排量
        $a = new \AuctionCore\Other\Address();
        $city_name = $a->get_name_by_code($car_info['location_area']);
        $map['car_location'] = $city_name;//车辆所在地
        $car_guishu = $a->get_name_by_code($car_info['reg_city']);
        $map['car_guishu'] = $car_guishu;//归属地
        $map['car_isloan'] = "NO";//有无购车贷款未还 yes or no
        $ximu = new XimuApi();
        $res = $ximu->_get("CREDIT", "queryalloworderbal", $map);
        return $res;
    }

    private function record_log($order_id, $car_id, $action)
    {
        //记录员工操作日志
        $log = [
            'emp_id' => $this->emp_id,
            'order_id' => $order_id,
            'car_id' => $car_id,
            'owner_id' => 0,
            'action' => $action
        ];
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name' => $action,
            'emp_id' => $this->emp_id
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);

        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => $action,
            'emp_id' => $this->emp_id
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);
    }


    //违约
    public function violate()
    {
        $order_id = I("post.order_id/d", 0);
        if ($order_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $o = new AuOrder($order_id);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $poInfo = $o->getPurchaseByOrderId();
        $c = new Car($orderInfo['car_id']);
        $carInfo = $c->detail();
        $car_plate_number = C('PLATE_PREFIX')[$carInfo['plate_prefix']] . $carInfo['plate_suffix'];
        $data = [];
        $data['order_id'] = $order_id;
        $data['order_no'] = $orderInfo['order_no'];
        $data['po_id'] = $poInfo['id'];
        $data['po_no'] = $poInfo['purchase_order_nu'];
        $data['sid'] = $poInfo['sid'];
        $data['car_plate_number'] = $car_plate_number;
        $data['order_status'] = $orderInfo['status'];
        $data['source'] = 6;
        $data['originator'] = $this->emp_id;
        $res = ChangeOrder::CreateChangeOrder($data);
        if (!$res) {
            $this->record_log($order_id, $orderInfo['car_id'], "过户发起违约");
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->helpJsonResult(200, '处理成功');
        }
    }

    public function dispute()
    {
        $order_id = I("get.order_id/d", 0);
        if ($order_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $o = new AuOrder($order_id);
        $orderInfo = $o->detail();
        $c = new Car($orderInfo['car_id']);
        $carInfo = $c->detail();
        $cm = new CarModel();
        $brand = $cm->get_model_by_id($carInfo['brand_id'], 'brand');
        $series = $cm->get_model_by_id($carInfo['series_id'], 'series');
        $model = $cm->get_model_by_id($carInfo['model_id']);
        $car_model = $brand['name'] . ' ' . $series['name'] . ' ' . $model['name'];
        $this->assign('order_id', $order_id);
        $this->assign('order_no', $orderInfo['order_no']);
        $this->assign('car_model', $car_model);
        $this->display();
    }

    //争议
    public function saveDispute()
    {
        $order_id = I("post.order_id/d", 0);
        if ($order_id < 1) {
            $this->helpJsonResult(400, "参数错误");
        }
        $remark = I("post.remark");
        $dispute_reason = I("post.dispute_reason");
        $o = new AuOrder($order_id);
        $orderInfo = $o->detail();
        if ($orderInfo['is_lock'] == 1) {
            $this->helpJsonResult(400, "拍单已锁定");
        }
        $poInfo = $o->getPurchaseByOrderId();
        $c = new Car($orderInfo['car_id']);
        $carInfo = $c->detail();
        $car_plate_number = C('PLATE_PREFIX')[$carInfo['plate_prefix']] . $carInfo['plate_suffix'];
        $data = [];
        $data['order_id'] = $order_id;
        $data['order_no'] = $orderInfo['order_no'];
        $data['po_id'] = $poInfo['id'];
        $data['po_no'] = $poInfo['purchase_order_nu'];
        $data['car_plate_number'] = $car_plate_number;
        $data['order_status'] = $orderInfo['status'];
        $data['od_id'] = $orderInfo['success_dealer_id'];
        $data['od_type'] = 1;
        $data['remark'] = $remark;
        $data['dispute_reason'] = $dispute_reason;
        $data['emp_id'] = $this->emp_id;
        $res = Dispute::DisputeAdd($data);
        if (!$res) {
            $this->helpJsonResult(400, '处理失败');
        } else {
            $this->record_log($order_id, $orderInfo['car_id'], "过户发起争议");
            $this->helpJsonResult(200, '处理成功');
        }
    }

}
